//
//  RequestError.m
//  项目三
//
//  Created by rimi1 on 2016/12/13.
//  Copyright © 2016年 Wangxingqian. All rights reserved.
//

#import "RequestError.h"

static RequestError *error = nil;

@implementation RequestError

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.label];
    }
    return self;
}

+ (instancetype)sharedError {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        error = [[RequestError alloc] initWithFrame:CGRectMake(0, 64, kWIDTH, 44)];
        error.backgroundColor = kCOLOR_RGB(255, 252, 229, 1);
        error.alpha = 0;
    });
    
    return error;
}

- (void)showErrorWithTitle:(NSString *)title {
    self.label.text = title;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [window addSubview:self];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1;
    } completion:^(BOOL finished) {
        [self hideAnimation];
    }];
}

- (void)hideAnimation {
    [UIView animateWithDuration:0.3 delay:1 options:UIViewAnimationOptionLayoutSubviews animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

#pragma mark - get

- (UILabel *)label {
    
    if (!_label) {
        
        _label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kWIDTH, 44)];
        _label.textAlignment = NSTextAlignmentCenter;
        
    }
    return _label;
}

@end










